package org.jivesoftware.smackx.pubsub;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
import org.jivesoftware.smackx.pubsub.packet.SyncPacketSend;
import org.jivesoftware.smackx.pubsub.util.NodeUtils;

public class PubSubManager
{
  protected Connection con;
  private Map<String, Node> nodeMap = new ConcurrentHashMap();
  protected String to;

  public PubSubManager(Connection paramConnection)
  {
    this.con = paramConnection;
  }

  public PubSubManager(Connection paramConnection, String paramString)
  {
    this.con = paramConnection;
    this.to = paramString;
  }

  static PubSub createPubsubPacket(String paramString, IQ.Type paramType, PacketExtension paramPacketExtension)
  {
    return createPubsubPacket(paramString, paramType, paramPacketExtension, null);
  }

  static PubSub createPubsubPacket(String paramString, IQ.Type paramType, PacketExtension paramPacketExtension, PubSubNamespace paramPubSubNamespace)
  {
    PubSub localPubSub = new PubSub();
    localPubSub.setTo(paramString);
    localPubSub.setType(paramType);
    if (paramPubSubNamespace != null)
      localPubSub.setPubSubNamespace(paramPubSubNamespace);
    localPubSub.addExtension(paramPacketExtension);
    return localPubSub;
  }

  static Packet sendPubsubPacket(Connection paramConnection, String paramString, IQ.Type paramType, PacketExtension paramPacketExtension)
    throws XMPPException
  {
    return sendPubsubPacket(paramConnection, paramString, paramType, paramPacketExtension, null);
  }

  static Packet sendPubsubPacket(Connection paramConnection, String paramString, IQ.Type paramType, PacketExtension paramPacketExtension, PubSubNamespace paramPubSubNamespace)
    throws XMPPException
  {
    return SyncPacketSend.getReply(paramConnection, createPubsubPacket(paramString, paramType, paramPacketExtension, paramPubSubNamespace));
  }

  static Packet sendPubsubPacket(Connection paramConnection, String paramString, IQ.Type paramType, PubSub paramPubSub)
    throws XMPPException
  {
    return sendPubsubPacket(paramConnection, paramString, paramType, paramPubSub, null);
  }

  static Packet sendPubsubPacket(Connection paramConnection, String paramString, IQ.Type paramType, PubSub paramPubSub, PubSubNamespace paramPubSubNamespace)
    throws XMPPException
  {
    return SyncPacketSend.getReply(paramConnection, paramPubSub);
  }

  private Packet sendPubsubPacket(IQ.Type paramType, PacketExtension paramPacketExtension)
    throws XMPPException
  {
    return sendPubsubPacket(paramType, paramPacketExtension, null);
  }

  private Packet sendPubsubPacket(IQ.Type paramType, PacketExtension paramPacketExtension, PubSubNamespace paramPubSubNamespace)
    throws XMPPException
  {
    return sendPubsubPacket(this.con, this.to, paramType, paramPacketExtension, paramPubSubNamespace);
  }

  public LeafNode createNode()
    throws XMPPException
  {
    NodeExtension localNodeExtension = (NodeExtension)((PubSub)sendPubsubPacket(IQ.Type.SET, new NodeExtension(PubSubElementType.CREATE))).getExtension("create", PubSubNamespace.BASIC.getXmlns());
    LeafNode localLeafNode = new LeafNode(this.con, localNodeExtension.getNode());
    localLeafNode.setTo(this.to);
    this.nodeMap.put(localLeafNode.getId(), localLeafNode);
    return localLeafNode;
  }

  public LeafNode createNode(String paramString)
    throws XMPPException
  {
    return ((LeafNode)createNode(paramString, null));
  }

  public Node createNode(String paramString, Form paramForm)
    throws XMPPException
  {
    Object localObject;
    PubSub localPubSub = createPubsubPacket(this.to, IQ.Type.SET, new NodeExtension(PubSubElementType.CREATE, paramString));
    boolean bool = true;
    if (paramForm != null)
    {
      localPubSub.addExtension(new FormNode(FormNodeType.CONFIGURE, paramForm));
      FormField localFormField = paramForm.getField(ConfigureNodeFields.node_type.getFieldName());
      if (localFormField != null)
        bool = ((String)localFormField.getValues().next()).equals(NodeType.leaf.toString());
    }
    sendPubsubPacket(this.con, this.to, IQ.Type.SET, localPubSub);
    if (bool)
      localObject = new LeafNode(this.con, paramString);
    while (true)
    {
      ((Node)localObject).setTo(this.to);
      this.nodeMap.put(((Node)localObject).getId(), localObject);
      return localObject;
      localObject = new CollectionNode(this.con, paramString);
    }
  }

  public void deleteNode(String paramString)
    throws XMPPException
  {
    sendPubsubPacket(IQ.Type.SET, new NodeExtension(PubSubElementType.DELETE, paramString), PubSubElementType.DELETE.getNamespace());
    this.nodeMap.remove(paramString);
  }

  public DiscoverItems discoverNodes(String paramString)
    throws XMPPException
  {
    DiscoverItems localDiscoverItems = new DiscoverItems();
    if (paramString != null)
      localDiscoverItems.setNode(paramString);
    localDiscoverItems.setTo(this.to);
    return ((DiscoverItems)SyncPacketSend.getReply(this.con, localDiscoverItems));
  }

  public List<Affiliation> getAffiliations()
    throws XMPPException
  {
    return ((AffiliationsExtension)((PubSub)sendPubsubPacket(IQ.Type.GET, new NodeExtension(PubSubElementType.AFFILIATIONS))).getExtension(PubSubElementType.AFFILIATIONS)).getAffiliations();
  }

  public ConfigureForm getDefaultConfiguration()
    throws XMPPException
  {
    return NodeUtils.getFormFromPacket((PubSub)sendPubsubPacket(IQ.Type.GET, new NodeExtension(PubSubElementType.DEFAULT), PubSubElementType.DEFAULT.getNamespace()), PubSubElementType.DEFAULT);
  }

  public Node getNode(String paramString)
    throws XMPPException
  {
    Object localObject = (Node)this.nodeMap.get(paramString);
    if (localObject == null)
    {
      DiscoverInfo localDiscoverInfo = new DiscoverInfo();
      localDiscoverInfo.setTo(this.to);
      localDiscoverInfo.setNode(paramString);
      if (!(((DiscoverInfo.Identity)((DiscoverInfo)SyncPacketSend.getReply(this.con, localDiscoverInfo)).getIdentities().next()).getType().equals(NodeType.leaf.toString())))
        break label111;
      localObject = new LeafNode(this.con, paramString);
    }
    while (true)
    {
      ((Node)localObject).setTo(this.to);
      this.nodeMap.put(paramString, localObject);
      return localObject;
      label111: localObject = new CollectionNode(this.con, paramString);
    }
  }

  public List<Subscription> getSubscriptions()
    throws XMPPException
  {
    return ((SubscriptionsExtension)sendPubsubPacket(IQ.Type.GET, new NodeExtension(PubSubElementType.SUBSCRIPTIONS)).getExtension(PubSubElementType.SUBSCRIPTIONS.getElementName(), PubSubElementType.SUBSCRIPTIONS.getNamespace().getXmlns())).getSubscriptions();
  }

  public DiscoverInfo getSupportedFeatures()
    throws XMPPException
  {
    return ServiceDiscoveryManager.getInstanceFor(this.con).discoverInfo(this.to);
  }
}